﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Hongapo_Common;
using System.Data.SqlClient;

namespace Hongapo_Services.Product
{
    public class ProductImage
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Image");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Hongapo_Model.Product.ProductImage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Image(");
            strSql.Append("OrderNo,SmallImg,BigImg,Sort,Isopen,Remark)");
            strSql.Append(" values (");
            strSql.Append("@OrderNo,@SmallImg,@BigImg,@Sort,@Isopen,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderNo", SqlDbType.VarChar,20),
					new SqlParameter("@SmallImg", SqlDbType.VarChar,100),
					new SqlParameter("@BigImg", SqlDbType.VarChar,100),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,50)};
            parameters[0].Value = model.OrderNo;
            parameters[1].Value = model.SmallImg;
            parameters[2].Value = model.BigImg;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.Isopen;
            parameters[5].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Hongapo_Model.Product.ProductImage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Image set ");
            strSql.Append("OrderNo=@OrderNo,");
            strSql.Append("SmallImg=@SmallImg,");
            strSql.Append("BigImg=@BigImg,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("Isopen=@Isopen,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderNo", SqlDbType.VarChar,20),
					new SqlParameter("@SmallImg", SqlDbType.VarChar,100),
					new SqlParameter("@BigImg", SqlDbType.VarChar,100),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,50),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.OrderNo;
            parameters[1].Value = model.SmallImg;
            parameters[2].Value = model.BigImg;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.Isopen;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateImageIsopen(string orderNo)
        {
            string sql = "update T_Image set isopen=1 where OrderNo='" + orderNo + "'";
            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 根据图片ID删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Image ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
           };
            parameters[0].Value = ID;
            DeleteImg(ID);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
              
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 根据产品编号删除图片
        /// </summary>
        public bool Delete(string OrderNo)
        {
            try
            {
                DataTable dt = GetList(" OrderNo='" + OrderNo + "'").Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    int id = TypeConvert.ToInt32(dr["ID"]);
                    Delete(id);
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
           
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void DeleteImg(int id)
        {
            string path = System.Web.HttpContext.Current.Server.MapPath("~/images/product/");
            Hongapo_Model.Product.ProductImage model = GetModel(id);
            string smallPath = path + model.SmallImg;
            string bigPath = path + model.BigImg;
            if (ImageHelper.ExistsFile(smallPath))
            {
                ImageHelper.DeleteFile(smallPath);
            }
            if (ImageHelper.ExistsFile(bigPath))
            {
                ImageHelper.DeleteFile(bigPath);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Hongapo_Model.Product.ProductImage GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,OrderNo,SmallImg,BigImg,Sort,Isopen,Remark from T_Image ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            Hongapo_Model.Product.ProductImage model = new Hongapo_Model.Product.ProductImage();
            DataSet ds = GetList(" ID="+ID+"");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNo"] != null && ds.Tables[0].Rows[0]["OrderNo"].ToString() != "")
                {
                    model.OrderNo = ds.Tables[0].Rows[0]["OrderNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SmallImg"] != null && ds.Tables[0].Rows[0]["SmallImg"].ToString() != "")
                {
                    model.SmallImg = ds.Tables[0].Rows[0]["SmallImg"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BigImg"] != null && ds.Tables[0].Rows[0]["BigImg"].ToString() != "")
                {
                    model.BigImg = ds.Tables[0].Rows[0]["BigImg"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Sort"] != null && ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Isopen"] != null && ds.Tables[0].Rows[0]["Isopen"].ToString() != "")
                {
                    model.Isopen = int.Parse(ds.Tables[0].Rows[0]["Isopen"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,OrderNo,SmallImg,BigImg,Sort,Isopen,Remark ");
            strSql.Append(" FROM T_Image ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,OrderNo,SmallImg,BigImg,Sort,Isopen,Remark ");
            strSql.Append(" FROM T_Image ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

  
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = strWhere;
            return DbHelperSQL.RunProcedure("P_ImageByPager", parameters, "ds");
        }

        #endregion  Method
    }
}
